<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>放大镜</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #smallImg {
            width: 300px;
            height: 300px;
            background-image: url(../img/2024-08-14-04-40-00-576x384.jpg);
            background-size: cover;
        }

        #smallDiv {
            width: 100px;
            height: 100px;
            background-color: rgba(121, 44, 44, 0.5);
            display: none;
            position: absolute;
        }

        #bigDiv {
            width: 300px;
            height: 300px;
            position: absolute;
            top: 0;
            left: 300px;
            overflow: hidden;
            display: none;
        }

        #bigImg {
            width: 900px;
            height: 900px;
            position: absolute;
        }
    </style>

</head>

<body>
    <div class="fang">
        <div id="smallImg">
            <div id="smallDiv"></div>
        </div>

        <div id="bigDiv">
            <img src="../img/2024-08-14-04-40-00-576x384.jpg" alt="" id="bigImg">
        </div>
    </div>

    <script>
        let smallImg = document.getElementById("smallImg");
        let smallDiv = document.getElementById("smallDiv");
        let bigDiv = document.getElementById("bigDiv");
        let bigImg = document.getElementById("bigImg");

        smallImg.onmouseover = function () {
            smallDiv.style.display = "block";
            bigDiv.style.display = "block";
        }

        smallImg.onmouseout = function () {
            smallDiv.style.display = "none";
            bigDiv.style.display = "none";
        }

        smallImg.onmousemove = function (e) {
            let x = e.clientX - 50;
            let y = e.clientY - 50;

            if (x >= 200) {
                x = 200;
            }

            if (x <= 0) {
                x = 0;
            }

            if (y > 200) {
                y = 200;
            }

            if (y <= 0) {
                y = 0;
            }

            smallDiv.style.left = x + "px";
            smallDiv.style.top = y + "px";

            bigImg.style.left = -3 * x + "px";
            bigImg.style.top = -3 * y + "px";
        }

    </script>
</body>

</html>